﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Strategy.Core.TradeTypes;

namespace Strategy.Core.TradeStreams
{
    public class CandleStream : Strategy.Core.TradeStreams.ITradeStream<Candle>
    {
        public static string generateID() { return "Candle stream"; }
        public override int getLevel()
        {
            return 0;
        }
        private CandleStream() { }
        public CandleStream(TimeSpan interval) 
        { 
            Interval = interval;
            values = new List<Candle>();
        }         
        public override void Update(QuoteData qd)
        {
            if (values.Count == 0)
                values.Add(new Candle(qd.Date, Interval));
            if (values.Last().OpenTime > qd.Date)
                throw new TimeoutException("QuoteData isn't newer");
            if (values.Last().CloseTime <= qd.Date)
            {
                values.Add(new Candle(values.Last().CloseTime, Interval));
                Update(qd);
            }
            else
                values.Last().Update(qd);
        }
        public override string getID()
        {
            return generateID();
        }
    }
}
